// 导入需要的config和request
import confing from '../../config/config.js';
import request from '../../util/request.js';


// 申明请求对象
const clientRequest = {
    // client请求的基本url
    BASE_URL: confing.SYS_URL + "/client",
    // /**
    //  * 保存客户信息
    //  * @param {*} client 
    //  * @param {*} callback 
    //  */
    // save(client,callback){
    //     if(client){
    //         request.post(this.BASE_URL + "/save",client,callback);
    //     }
    // },
    // 请求的API
    list(page = 1, limit = 10, client, callbcak) {
        // url
        let url = this.BASE_URL + "/list?page=" + page + "&limit=" + limit;
        if (client) {
            if (client.clientCode) {
                url += "&clientCode=" + client.clientCode;
            }
            if (client.clientName) {
                url += "&clientName=" + client.clientName;
            }
            if (client.clientNick) {
                url += "&clientNick=" + client.clientNick;
            }
            if (client.clientEn) {
                url += "&clientEn=" + client.clientEn;
            }
            if (client.enableFlag) {
                url += "&enableFlag=" + client.enableFlag;
            }
        }
        // 发送请求
        request.get(url, callbcak);
    },


    /**
 * 保存客户
 * @param {*} client  客户对象
 * @param {*} callback  回调函数
 */
    saveClient(client, callback) {
        let url = this.BASE_URL + "/save";
        request.post(url, client, callback);
    },


    /**
 * 根据id加载客户信息
 * @param {*} clientId 客户编号
 * @param {*} callback 回调函数
 */
    get(clientId, callback) {
        if (clientId) {
            // 准备url
            let url = this.BASE_URL + "/get?clientId=" + clientId;
            request.get(url, callback);
        }
        // 如果没有clientId，不做任何处理
    },
    /**
 * 根据ID编辑客户信息
 * @param {} client 
 * @param {*} callback  
 */
    edit(client, callback) {
        if (client != undefined && client.clientId != undefined) {
            request.put(this.BASE_URL + "/update", client, callback);
        }
    },
        /**
     * 根据ID删除客户
     * @param {*} clientId 
     * @param {*} callback 
     */
        delete(clientId,callback){
            request.delete(this.BASE_URL + "/" + clientId,callback);
        },
    /**
     * 根据ID批量删除客户,url的遍历,和后端没关系
     * @param {*} clientIds
     * @param {*} callback 
     */
    deleteSelect(clientIds, callback) {
        // 拼接URL
        let url = this.BASE_URL + "/deletes?t=1";
        if (clientIds != undefined) {
            clientIds.forEach(clientId => {
                url += "&clientId=" + clientId;
            });//看这个字段&clientId=:
            request.delete(url, callback);
        }
    },



}

// 将clientRequest暴漏出去
export default clientRequest;